﻿/**
 * @created 13-Mar-2006 10:41:20 p.m.
 * @version 0.0.1
 */
import SCUMMCore.*;
import SCUMMCore.Nodes.*;
import SCUMMCharacter.*;
import SCUMMCharacter.States.*;

class SCUMMCharacter.States.SCUMMActorStateWalking implements SCUMMState {
	
	var nXFinal:Number;
	var nYFinal:Number;
	
	//Solved Path, usefull for pathfinding
	var path_Solved:NodeCollection;
	var currentNode:Node;
	//Timer for skip phase proposes
	private var lastClicksTakenForSkip:Number;
	//priority of the state
	var intPriotity:Number;
	var actor:SCUMMActor;
	//Point to Walk
	public var targetPositionToWalk:Node;
	//Stores the number of element in the arrTextToSay var wich the actor is talking now.
	public var velocidad:Number;
	public var versorX:Number;
	public var versorY:Number;
	public var norma:Number;
	function SCUMMActorStateWalking(actor:SCUMMActor) {
		lastClicksTakenForSkip = 0;
		this.actor = actor;
		velocidad = 10;
		//priority of the state says if a state can be override by another
		//if the action is uncompleted. The higer the priority, 
		//most unoverrideable is the state
		intPriotity = 1;
		path_Solved = null;
	}
	/**
	 *@return intPriotity
	 */
	public function getIntPriotity():Number {
		return intPriotity;
	}
	public function handle():Void
	{
		if ( targetPositionToWalk )
		{
			setVersorsVars();
			if (norma>velocidad && norma!=0)
			{
				if (isInWalkableArea())
				{
					actor.mc._x += versorX * velocidad;
					actor.mc._y += versorY * velocidad;
				}
				else
				{
					path_Solved = this.actor.scene.findPath(targetPositionToWalk._x, targetPositionToWalk._y, actor.mc._x, actor.mc._y);
					
					if (path_Solved.getCount() > 1)
					{
						path_Solved.addNode(targetPositionToWalk);
						currentNode = path_Solved.getFirstNode();
						targetPositionToWalk =  currentNode;
					}
					else
					{
						targetPositionToWalk = null;
						path_Solved = null;
					}
				}
			}
			else
			{
				if (isInWalkableArea() || norma==0)
				{
					actor.mc._x = targetPositionToWalk._x;
					actor.mc._y = targetPositionToWalk._y;
					targetPositionToWalk = null;
				}
			}
		}
		else if (path_Solved)
		{
			//targetPositionToWalk = path_Solved.getPrevNode(targetPositionToWalk);
			targetPositionToWalk = path_Solved.getNextNode(currentNode);
			currentNode = targetPositionToWalk;
			if (!targetPositionToWalk)
			{
				targetPositionToWalk = null;
				path_Solved = null;
				actor.state = null;
			}
		}
		else
		{
			targetPositionToWalk = null;
			path_Solved = null;
			actor.state = null;
		}
	}
	public function skipCurrentPhase():Void {
		var nOffSet:Number = this.nXFinal - this.actor.mc._x;
		this.targetPositionToWalk = null;
		this.path_Solved = null;
		this.actor.state = null;
		this.actor.mc._x = this.nXFinal;
		this.actor.mc._y = this.nYFinal;
		this.nXFinal = undefined;
		this.nYFinal = undefined;
	}
	/**
	 * Sets the versorX and VersorY vars
	 */
	public function setVersorsVars() {
		versorX = targetPositionToWalk._x - actor.mc._x;
		versorY = targetPositionToWalk._y - actor.mc._y;
		norma = Math.sqrt(versorX*versorX+versorY*versorY);
		versorY /= norma;
		versorX /= norma;
		actor.mc._versorX = versorX;
		actor.mc._versorY = versorY;
	}
	/**
	 * Check if actor is inside the walkable area
	 */
	function isInWalkableArea():Boolean {
		
		var actorPoint:Object = {
			x:actor.mc._x + versorX * velocidad,
			y:actor.mc._y + versorY * velocidad
		};
		this.actor.scene.mc.localToGlobal(actorPoint);
		
		return this.actor.scene.walkableArea.hitTest(
			actorPoint.x,
			actorPoint.y,
			true);
	}
	public function setTargetPointToWalk(x:Number, y:Number) {
		this.nXFinal = x;
		this.nYFinal = y;
		
		var tempObj = new Object();
		tempObj._x = x;
		tempObj._y = y;
		targetPositionToWalk = new Node(tempObj);
	}
}
